﻿//***************************************************************
// 脚本名称：Entity.cs
// 类创建人：周  波
// 创建日期：2017.02
// 功能描述：游戏实体基类
//***************************************************************

using UnityEngine;

/// <summary>
/// 实体初始化参数
/// </summary>
public class EntityInitializeParam : GameSpriteInitializeParam
{
    /// <summary>
    /// 实例id
    /// </summary>
    public ulong EntityUUID;
    /// <summary>
    /// 实体类型
    /// </summary>
    public EntityType EntityType;
    /// <summary>
    /// 实体名字
    /// </summary>
    public string EntityName;
}

public abstract class Entity : GameSprite
{
    /// <summary>
    /// 实例id
    /// </summary>
    public ulong EntityUUID { get; protected set; }
    /// <summary>
    /// 名字
    /// </summary>
    public string EntityName { get; protected set; }

    /// <summary>
    /// 实体对应的挂节点
    /// </summary>
    public JointPoints JointPoints { get; protected set; }

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="param">初始化参数</param>
    /// <returns>true：成功；false：失败</returns>
    public virtual bool Initialize(EntityInitializeParam param)
    {
        this.EntityUUID = param.EntityUUID;
        this.EntityName = param.EntityName;
        base.InitializeSprite(param);
        return true;
    }

    /// <summary>
    /// 销毁
    /// </summary>
    public virtual void Uninitialize()
    {
        RemoveTipsInfo();// 销毁时删除提示信息
        base.UninitializeSprite();
    }

    /// <summary>
    /// 获取到实体类型
    /// </summary>
    /// <returns>实体类型</returns>
    public abstract EntityType GetEntityType();

    /// <summary>
    /// 重载 MonoBehaviour 的 Update 方法
    /// </summary>
    protected virtual void Update()
    {
        UpdateTipsInfo();
    }

    /// <summary>
    /// 设置位置--刷新高度
    /// </summary>
    public void ReSetPosition()
    {
        SetPosition(MyTransform.position);
    }

    /// <summary>
    /// 重载设置坐标，根据地形调整高度(y坐标)
    /// </summary>
    /// <param name="position"></param>
    public override void SetPosition(Vector3 position)
    {
        this.SetPosition(position.x, position.y, position.z);
    }

    /// <summary>
    /// 重载设置坐标，根据地形调整高度(y坐标)
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="z"></param>
    public override void SetPosition(float x, float y, float z)
    {
        float terrainHeight = Utility.GetTerrainHeight(x, y, z);
        terrainHeight = float.IsInfinity(terrainHeight) ? y : terrainHeight;
        base.SetPosition(x, terrainHeight, z);
    }

    protected override void LoadModelComplated(GameObject model)
    {
        base.LoadModelComplated(model);
        // 获取实体的挂节点
        JointPoints = model.GetComponent<JointPoints>();
        // 加载完成了更新一次提示信息
        UpdateTipsInfo();
    }

    /// <summary>
    /// 更新提示信息
    /// </summary>
    protected virtual void UpdateTipsInfo()
    {
        //if (MapTipsUI.Instance != null && JointPoints != null)
        //{
        //    if (JointPoints.GetJointTransform(JointNamesConst.Name) != null)
        //    {
        //        MapTipsUI.Instance.AddEntityTipsItem(InstanceID, this.EntityName, this.JointPoints.GetJointTransform(JointNamesConst.Name).position);
        //    }
        //}
    }

    /// <summary>
    /// 删除提示信息
    /// </summary>
    protected virtual void RemoveTipsInfo()
    {
        //if (MapTipsUI.Instance != null)
        //{
        //    MapTipsUI.Instance.RemoveEnitityTipsItem(InstanceID);
        //}
    }

    protected override void SetLayer()
    {
        Utility.SetGameObjectLayer(MyGameObject, "Sprite");
    }
}
